//
//  AUNavController.swift
//  AwesomeUMainModule_Example
//
//  Created by Miss_Special on 2018/4/11.
//  Copyright © 2018 CocoaPods. All rights reserved.
//

import UIKit

public class AUNavController: UINavigationController {

    override public func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加全屏滑动返回手势
        addFullScreenPopGesture()
    }
}

extension AUNavController {
    /// push隐藏底部tabbar
    override public func pushViewController(_ viewController: UIViewController, animated: Bool) {
        viewController.hidesBottomBarWhenPushed = viewControllers.count > 0 ? true : false
        super.pushViewController(viewController, animated: animated)
    }
}

// MARK:- <UIGestureRecognizerDelegate>
extension AUNavController: UIGestureRecognizerDelegate {
    
    /// 添加全屏滑动返回手势
    func addFullScreenPopGesture() {
        interactivePopGestureRecognizer?.isEnabled = false
        let action = Selector(("handleNavigationTransition:"))
        let gesture = UIPanGestureRecognizer(target: interactivePopGestureRecognizer?.delegate, action: action)
        view.addGestureRecognizer(gesture)
        gesture.delegate = self
    }
    
    /// 处理滑动返回手势的边界问题
    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count > 1, gestureRecognizer.isKind(of: UIPanGestureRecognizer.self), let gesture = gestureRecognizer as? UIPanGestureRecognizer {
            let point = gesture.translation(in: view)
            if point.x > 0 {
                return true
            } else {
                return false
            }
        } else {
            return false
        }
    }
}
